package com.ruoyi.attendance.controller;

import com.ruoyi.attendance.domain.Attendance;
import com.ruoyi.attendance.domain.AttendanceVO;
import com.ruoyi.attendance.service.AttendanceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/attendance")
public class AttendanceController extends BaseController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 查询考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(Attendance attendance)
    {
        startPage();
        List<Attendance> list = attendanceService.selectAttendanceList(attendance);
        return getDataTable(list);
    }


    /**
     * 获取考勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attendanceService.selectAttendanceById(id));
    }

    /**
     * 新增考勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:add')")
    @GetMapping
    public void add()
    {
        attendanceService.insertAttendance();
    }

    /**
     * 修改考勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Attendance attendance)
    {
        return toAjax(attendanceService.updateAttendance(attendance));
    }

    /**
     * 删除考勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attendanceService.deleteAttendanceByIds(ids));
    }


    /**
     * 获取状态统计getAttendanceStats
     */
    @GetMapping("/getAttendanceStats")
    public AttendanceVO getAttendanceStats() {
        Long userId = SecurityUtils.getUserId();
        AttendanceVO attendanceVO = attendanceService.getAttendanceStats(userId);
        System.out.println(attendanceVO);
        return attendanceVO;
    }
}
